package com.xkstream.client.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseRecycleViewAdapter;
import com.xkstream.client.entity.kt.MessagesData;
import com.xkstream.client.utils.DateUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017-9-26.
 */

public class MessageAdapter extends BaseRecycleViewAdapter {

    private List<MessagesData.Message> mData;

    public MessageAdapter(Context mContext, List<MessagesData.Message> data) {
        super(mContext);
        mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_message_item, parent, false);
        RecyclerView.ViewHolder holder = new ItemHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindItemData((ItemHolder) holder, position);
    }

    private void onBindItemData(ItemHolder holder, int pos) {
        MessagesData.Message message = mData.get(pos);
        holder.tvTime.setText(DateUtil.formatTime(message.getCreate_date(),"yyyy-MM-dd"));
        holder.tvTitle.setText(message.getTitle());
        holder.tvContent.setText(message.getContent());

        holder.itemView.setOnClickListener((v) -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvTime)
        TextView tvTime;
        @BindView(R.id.tvTitle)
        TextView tvTitle;
        @BindView(R.id.tvContent)
        TextView tvContent;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
